package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.DepartmentDao;
import com.zja.model.Department;

import java.util.List;

/**
 * @author JA
 * @date: 2025/4/28 15:45
 */
public class DepartmentDaoImpl extends BaseDAO implements DepartmentDao {
    @Override
    public List<Department> selectAll() {
        String sql = "SELECT * FROM department";
        try {
            return executeQuery(Department.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Department selectById(Long id) {
        try {
            String sql = "SELECT * FROM department WHERE id = ?";
            return executeQueryBean(Department.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Department department) {
        try {
            String sql = "INSERT INTO department (name, organization_id, manager_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)";
            return executeUpdate(sql, department.getName(), department.getOrganizationId(), department.getManagerId(), department.getCreatedAt(), department.getUpdatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Department department) {
        try {
            String sql = "UPDATE department SET name = ?, organization_id = ?, manager_id = ?, updated_at = ? WHERE id = ?";
            return executeUpdate(sql, department.getName(), department.getOrganizationId(), department.getManagerId(), department.getUpdatedAt(), department.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM department WHERE id = ?";
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
